<template>
  <div v-if="getShow">
    <LoginFormTitle class="enter-x" />
    <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef">
      <FormItem name="name" label="企业名称" :required=true class="enter-x">
        <Input
          class="fix-auto-fill"
          size="large"
          label="企业名称"
          v-model:value="formData.name"
        />
      </FormItem>
      <FormItem name="uniform_social_credit_code" label="社会统一信用代码" :required=true class="enter-x">
        <Input
          size="large"
          v-model:value="formData.uniform_social_credit_code"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem name="business_license" label="营业执照" :required=true class="enter-x">
        <Input
          size="large"
          class="fix-auto-fill"
          v-model:value="formData.business_license"
        />
      </FormItem>
      <FormItem name="account_username" label="账号" :required=true class="enter-x">
        <Input
          size="large"
          v-model:value="formData.account_username"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem name="account_password" label="密码" :required=true class="enter-x">
        <InputPassword
          size="large"
          v-model:value="formData.account_password"
        />
      </FormItem>
      <FormItem name="contact_name" label="联系人" :required=true class="enter-x">
        <Input
          size="large"
          v-model:value="formData.contact_name"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem name="contact_phone_number" label="联系电话" :required=true class="enter-x">
        <Input
          size="large"
          v-model:value="formData.contact_phone_number"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem name="contact_email" label="联系邮箱" :required=true class="enter-x">
        <Input
          size="large"
          v-model:value="formData.contact_email"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem name="logo" label="Logo" class="enter-x">
        <Input
          size="large"
          v-model:value="formData.logo"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem name="nature" label="性质" class="enter-x">
        <Select
          size="large"
          v-model:value="formData.nature"
          class="fix-auto-fill"
          :options="natureOptions"
        />
      </FormItem>
      <FormItem name="scale" label="规模" class="enter-x">
        <Select
          size="large"
          v-model:value="formData.scale"
          class="fix-auto-fill"
          :options="scaleOptions"
        />
      </FormItem>
      <FormItem name="main_business" label="主营业务" class="enter-x">
        <Input
          size="large"
          v-model:value="formData.main_business"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem name="website" label="官网" class="enter-x">
        <Input
          size="large"
          v-model:value="formData.website"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem name="desc" label="简介" class="enter-x">
        <Input
          size="large"
          v-model:value="formData.desc"
          class="fix-auto-fill"
        />
      </FormItem>

      <Button
        type="primary"
        class="enter-x"
        size="large"
        block
        @click="handleRegister"
        :loading="loading"
      >
        {{ t('sys.login.registerButton') }}
      </Button>
      <Button size="large" block class="mt-4 enter-x" @click="handleBackLogin">
        {{ t('sys.login.backSignIn') }}
      </Button>
    </Form>
  </div>
</template>
<script lang="ts" setup>
  import { reactive, ref, unref, computed } from 'vue';
  import LoginFormTitle from './LoginFormTitle.vue';
  import { Form, Input, Button, Select } from 'ant-design-vue';
  import { useI18n } from '@/hooks/web/useI18n';
  import { useLoginState, useFormRules, useFormValid, LoginStateEnum } from './useLogin';

  const FormItem = Form.Item;
  const InputPassword = Input.Password;
  const { t } = useI18n();
  const { handleBackLogin, getLoginState } = useLoginState();

  const formRef = ref();
  const loading = ref(false);
  const formData = reactive({
    name: '',
    account_username: '',
    account_password: '',
    uniform_social_credit_code: '',
    business_license: '',
    contact_name: '',
    contact_phone_number: '',
    contact_email: '',
    logo: '',
    nature: '',
    scale: '',
    main_business: '',
    website: '',
    desc: ''
  });

  const { getFormRules } = useFormRules(formData);
  const { validForm } = useFormValid(formRef);

  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.REGISTER);

  const natureOptions = [
    {
      id: 'private',
      label: '私营企业',
      value: 'private',
      key: 'private',
    },
    {
      id: 'state_owned',
      label: '国有企业',
      value: 'state_owned',
      key: 'state_owned',
    },
    {
      id: 'joint_ventures',
      label: '合资企业',
      value: 'joint_ventures',
      key: 'joint_ventures',
    },
    {
      id: 'foreign_funded',
      label: '外资企业',
      value: 'foreign_funded',
      key: 'foreign_funded',
    },
  ];

  const scaleOptions = [
    {
      id: '<20',
      label: '20人以下',
      value: '<20',
      key: '<20',
    },
    {
      id: '20-100',
      label: '20~100人',
      value: '20-100',
      key: '20-100',
    },
    {
      id: '100-500',
      label: '100~500人',
      value: '100-500',
      key: '100-500',
    },
    {
      id: '>500',
      label: '500人以上',
      value: '>500',
      key: '>500',
    },
  ];

  async function handleRegister() {
    const data = await validForm();
    if (!data) return;
  }
</script>
